gdal2Coordinates collection provides utilities for converting between different coordinate systems based on the geospatial information embedded in raster files.
Overview
These scripts read the coordinate system and geotransformation matrix from a GDAL-supported raster file to perform coordinate transformations. All conversions use the projection information from the input image.Available Scripts
pixel2longlat.py
Converts pixel coordinates to latitude/longitude (geographic) coordinates.The pixel/sample position (column number)
The line position (row number)
Path to the input raster file
The script calculates coordinates for the center of the specified pixel by applying a half-pixel shift.
pixel2meters.py
Converts pixel coordinates to projected coordinates in meters (or feet, depending on the projection).The pixel/sample position (column number)
The line position (row number)
Path to the input raster file
longlat2meters.py
Converts longitude/latitude coordinates to projected coordinates (X, Y) in meters or feet.Longitude in decimal degrees
Latitude in decimal degrees
Path to the input raster file (provides projection information)
meters2longlat.py
Converts projected coordinates (X, Y) in meters or feet to longitude/latitude.X coordinate in meters (or feet)
Y coordinate in meters (or feet)
Path to the input raster file (provides projection information)
Implementation Details
Coordinate Transformation Process
All scripts follow a similar workflow:Read geotransformation matrix
Extract the affine transformation coefficients that map between pixel and ground coordinates
Create spatial reference objects
Build OGR spatial reference objects from the raster’s projection information
Transform coordinates
Apply the appropriate transformation using GDAL/OGR coordinate transformation functions
Geotransformation Matrix
GDAL uses a 6-coefficient affine transformation:geomatrix[0]: Top-left X coordinategeomatrix[1]: Pixel width (X resolution)geomatrix[2]: Row rotation (typically 0)geomatrix[3]: Top-left Y coordinategeomatrix[4]: Column rotation (typically 0)geomatrix[5]: Pixel height (Y resolution, typically negative)
Requirements
- Modern:
from osgeo import gdal, osr - Legacy:
import gdal(automatically detected)
Use Cases
Interactive Mapping
Convert mouse clicks on images to geographic coordinates for display
Data Integration
Transform coordinates between datasets with different projections
Ground Control
Identify ground coordinates for surveyed pixel locations
Quality Control
Verify georeferencing accuracy at known control points
Credits
Based ontolatlong.py by Andrey Kiselev (dron@remotesensing.org). Extended for meter/feet conversions by Trent Hare (USGS).
These scripts are part of the GDAL Python samples and are released under the MIT License.